package com.todo.strategy.offline.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.offline.infrastructure.convert.StrategyConvert;
import com.todo.strategy.offline.infrastructure.entity.condition.PlatformStrategyCondition;
import com.todo.strategy.offline.infrastructure.entity.domain.PlatformStrategyDO;
import com.todo.strategy.offline.infrastructure.entity.po.PlatformStrategyDetailPO;
import com.todo.strategy.offline.infrastructure.entity.po.PlatformStrategyPO;
import com.todo.strategy.offline.infrastructure.mapper.PlatformStrategyDetailMapper;
import com.todo.strategy.offline.infrastructure.mapper.PlatformStrategyMapper;
import com.todo.strategy.offline.infrastructure.repository.PlatformStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PlatformStrategy)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("platformStrategyRepository")
public class PlatformStrategyRepositoryImpl implements PlatformStrategyRepository {
    @Resource
    private PlatformStrategyMapper platformStrategyMapper;
    @Resource
    private PlatformStrategyDetailMapper platformStrategyDetailMapper;


    @Override
    public Boolean insertPlatformStrategyInfo(PlatformStrategyPO platformStrategyPO) {
        if (platformStrategyPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (platformStrategyPO.getTitle() == null || platformStrategyPO.getRecommendDesc() == null || platformStrategyPO.getStrategyType() == null || platformStrategyPO.getTravelTimeType() == null || platformStrategyPO.getStatus() == null) {
            log.warn("PlatformStrategyRepository#insertPlatformStrategyInfo 攻略基础信息字段校验失败，默认操作失败 platformStrategyPO：【{}】", JSONObject.toJSONString(platformStrategyPO));
            return false;
        }
        int strategyResult = platformStrategyMapper.insertPlatformStrategyPO(platformStrategyPO);
        if (strategyResult == 0) {
            log.warn("PlatformStrategyRepository#insertPlatformStrategy 新增平台攻略信息失败，攻略信息新增失败 platformStrategyPO：【{}】", JSONObject.toJSONString(platformStrategyPO));
            return false;
        }
        return true;
    }

    @Override
    public Boolean insertPlatformStrategyDetail(PlatformStrategyDetailPO platformStrategyDetailPO) {
        if (platformStrategyDetailPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (platformStrategyDetailPO.getAgeSuggest() == null || platformStrategyDetailPO.getSecuritySuggest() == null) {
            log.warn("PlatformStrategyRepository#insertPlatformStrategyDetail 攻略详情信息字段校验失败，默认操作失败 platformStrategyDetailPO：【{}】", JSONObject.toJSONString(platformStrategyDetailPO));
            return false;
        }
        int detailResult = platformStrategyDetailMapper.insertPlatformStrategyDetailPO(platformStrategyDetailPO);
        if (detailResult == 0) {
            log.warn("PlatformStrategyRepository#insertPlatformStrategy 新增平台攻略信息失败，攻略详情新增失败 platformStrategyDetailPO：【{}】", JSONObject.toJSONString(platformStrategyDetailPO));
            throw new BizException(BizErrorCodeEnum.SYSTEM_ERROR);
        }
        if (platformStrategyDetailPO.getAgeSuggest() == null || platformStrategyDetailPO.getSecuritySuggest() == null) {
            log.warn("PlatformStrategyRepository#insertPlatformStrategyDetail 攻略详情信息字段校验失败，默认操作失败 platformStrategyDetailPO：【{}】", JSONObject.toJSONString(platformStrategyDetailPO));
            return false;
        }
        return true;
    }

    @Override
    public Boolean updatePlatformStrategyInfo(PlatformStrategyPO platformStrategyPO) {
        if (platformStrategyPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (platformStrategyPO.getTitle() == null || platformStrategyPO.getRecommendDesc() == null || platformStrategyPO.getStrategyType() == null || platformStrategyPO.getTravelTimeType() == null || platformStrategyPO.getStatus() == null) {
            log.warn("PlatformStrategyRepository#updatePlatformStrategyInfo 攻略基础信息字段校验失败，默认操作失败 platformStrategyPO：【{}】", JSONObject.toJSONString(platformStrategyPO));
            return false;
        }
        int strategyResult = platformStrategyMapper.updatePlatformStrategyPOById(platformStrategyPO);
        if (strategyResult == 0) {
            log.warn("PlatformStrategyRepository#updatePlatformStrategyById 更新平台攻略信息失败，攻略信息更新失败 platformStrategyPO：【{}】", JSONObject.toJSONString(platformStrategyPO));
            return false;
        }

        return true;
    }

    @Override
    public Boolean updatePlatformStrategyDetail(PlatformStrategyDetailPO platformStrategyDetailPO) {
        if (platformStrategyDetailPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (platformStrategyDetailPO.getAgeSuggest() == null || platformStrategyDetailPO.getSecuritySuggest() == null) {
            log.warn("PlatformStrategyRepository#updatePlatformStrategyDetail 攻略详情信息字段校验失败，默认操作失败 platformStrategyDetailPO：【{}】", JSONObject.toJSONString(platformStrategyDetailPO));
            return false;
        }
        int detailResult = platformStrategyDetailMapper.updatePlatformStrategyDetailPOById(platformStrategyDetailPO);
        if (detailResult == 0) {
            log.warn("PlatformStrategyRepository#updatePlatformStrategyById 更新平台攻略信息失败，攻略详情更新失败 platformStrategyDetailPO：【{}】", JSONObject.toJSONString(platformStrategyDetailPO));
            throw new BizException(BizErrorCodeEnum.SYSTEM_ERROR);
        }
        return true;
    }


    @Override
    @Transactional
    public Boolean removePlatformStrategyById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlatformStrategyPO platformStrategyPO = queryBasePlatformStrategyById(id);
        if (platformStrategyPO == null) {
            return true;
        }
        int strategyResult = platformStrategyMapper.removePlatformStrategyPOById(id);
        if (strategyResult == 0) {
            log.warn("PlatformStrategyRepository#removePlatformStrategyById 删除平台攻略信息失败，攻略信息删除失败 id：【{}】", id);
            return false;
        }
        int detailResult = platformStrategyDetailMapper.removePlatformStrategyDetailPOById(id);
        if (detailResult == 0) {
            log.warn("PlatformStrategyRepository#removePlatformStrategyById 删除平台攻略信息失败，攻略详情删除失败 id：【{}】", id);
            throw new BizException(BizErrorCodeEnum.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public PlatformStrategyDO queryPlatformStrategyById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        PlatformStrategyPO platformStrategyPO = platformStrategyMapper.queryPlatformStrategyPOById(id);
        if (platformStrategyPO == null) {
            return null;
        }
        PlatformStrategyDetailPO platformStrategyDetailPO = platformStrategyDetailMapper.queryPlatformStrategyDetailPOById(id);
        PlatformStrategyDO platformStrategyDO = StrategyConvert.convertToPlatformStrategyDO(platformStrategyPO, platformStrategyDetailPO);
        return platformStrategyDO;
    }

    @Override
    public PlatformStrategyPO queryBasePlatformStrategyById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        PlatformStrategyPO platformStrategyPO = platformStrategyMapper.queryPlatformStrategyPOById(id);
        return platformStrategyPO;
    }

    @Override
    public PlatformStrategyDetailPO queryPlatformStrategyDetailById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        PlatformStrategyDetailPO platformStrategyDetailPO = platformStrategyDetailMapper.queryPlatformStrategyDetailPOById(id);
        return platformStrategyDetailPO;
    }

    @Override
    public PageResult<PlatformStrategyPO> queryPlatformStrategyPOByCondition(PlatformStrategyCondition platformStrategyCondition) {
        PageResult<PlatformStrategyPO> pageResult = new PageResult<>();
        if (platformStrategyCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(platformStrategyCondition.getPageIndex());
        pageResult.setPageSize(platformStrategyCondition.getPageSize());
        long count = queryCountByCondition(platformStrategyCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, platformStrategyCondition.getPageSize()));
        pageResult.setTotal(count);
        List<PlatformStrategyPO> sightPoiPOList = platformStrategyMapper.queryPlatformStrategyPOByCondition(platformStrategyCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= platformStrategyCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(PlatformStrategyCondition platformStrategyCondition) {
        if (platformStrategyCondition == null || platformStrategyCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return platformStrategyMapper.queryCountByCondition(platformStrategyCondition);
    }

    @Override
    public List<PlatformStrategyPO> queryPlatformStrategyByIdList(List<Long> strategyIdList) {
        if (CollectionUtils.isEmpty(strategyIdList)){
            return Lists.newArrayList();
        }
        return platformStrategyMapper.queryPlatformStrategyByIdList(strategyIdList);
    }

    @Override
    public Boolean updatePlatformStrategyStatus(Long strategyId, StrategyStatusEnum statusEnum) {
        if (strategyId == null || strategyId < 0 || statusEnum == null){
            return false;
        }
        return platformStrategyMapper.updatePlatformStrategyStatus(strategyId,statusEnum.getType());
    }

}
